﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.Common;
using System.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;

using Dapper;

namespace Gallery.DAL.Repositories
{
    public abstract class BaseRepository<T> : IDisposable
    where T : new()
    {
        protected virtual string TableName { get { return String.Empty; } }
        private string connectionName = "Gallery";
        protected IDbConnection Connection { get; set; }
        private IDbTransaction Transaction { get; set; }
        private bool commited = false;
        protected Database db { get; set; }

        public BaseRepository()
        {
            var connStr = ConfigurationManager.ConnectionStrings[connectionName].ConnectionString;
            this.db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
            this.Connection = db.CreateConnection();
            this.Connection.Open();
            //this.Connection.Execute("SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;");
            //this.Transaction = this.Connection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
        }

        public BaseRepository(Database db)
        {
            var connStr = ConfigurationManager.ConnectionStrings[connectionName].ConnectionString;
            this.db = db;
            //this.Connection = new SqlConnection(connStr);
            //this.Connection.Open();
            //this.Transaction = this.Connection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
        }

        public void Commit()
        {
            if (!commited && Transaction != null)
            {
                Transaction.Commit();
                commited = true;
            }
        }

        public void Dispose()
        {
            if (!commited && Transaction != null)
            {
                Transaction.Commit();
            }

            if (Connection.State == ConnectionState.Open)
            {
                Connection.Close();
            }
        }

        public virtual void Insert(T newObject)
        {
        }

        public virtual T Find(int id)
        {
            return Connection.Query<T>("SELECT * FROM " + TableName + " (nolock) WHERE id = @id", new { id = id }).Single();
        }


        public virtual IEnumerable<T> FindAll()
        {
            return Connection.Query<T>("SELECT * FROM " + TableName + " (nolock)");
        }

        public virtual IEnumerable<T> FindAll(string sortField, string sortOrder, int pageNum, int pageSize)
        {
            return null;
        }

        public virtual void Delete(int id)
        {
            Connection.Execute("DELETE FROM  " + TableName + " WHERE id = @id", new { id = id });
        }


        public virtual void Update(T updateObject)
        {
        }
    }
    
}